package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.WmWarehouse;
import com.pl.service.WmWarehouseService;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/4/26 20:59
 * @注释
 */
public class WmWarehouseReadLister extends AnalysisEventListener<WmWarehouse> {
    //定义批量导入的时候 每次导入几条数据
    private static final int BATCH_COUNT=5;
    //创建一个List集合存放接受的数据
    List<WmWarehouse> list=new ArrayList<>();
    //引入 service层
    private WmWarehouseService warehouseService;

    public WmWarehouseReadLister() {
    }

    public WmWarehouseReadLister(WmWarehouseService warehouseService) {
        this.warehouseService = warehouseService;
    }

    @Override
    public void invoke(WmWarehouse wmWarehouse, AnalysisContext analysisContext) {
        list.add(wmWarehouse);
        if (list.size()>=BATCH_COUNT){
            DateSave();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (list.size()>0){
            DateSave();
            list.clear();
        }
    }

    public void DateSave(){
        System.out.println("开始批量导入");
        warehouseService.insertBatch(list);
        System.out.println("结束批量导入");
    }
}
